﻿using System;
using System.Collections.Generic;
using DosTexPrj.Core.Models;
using NHibernate;
using NHibernate.Cfg;
using NUnit.Framework;

namespace DosTexPrj.Implement.NHibernate.Test.Repositories
{
    public class RepositoryFixtureBase
    {
        protected ISessionFactory _sessionFactory;
        protected ISession _session;

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            BootTrapper.BootAll();
            Configuration config = MyServiceLocator.DoGetInstance<Configuration>();
            this._sessionFactory = MyServiceLocator.DoGetInstance<ISessionFactory>();
            this._session = this._sessionFactory.OpenSession();
            Context();
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            this._session.Close();
            this._session.Dispose();
            this._sessionFactory.Dispose();
        }

        protected virtual void Context() { }

        protected Customer CreateDummyCustomer(string customerName)
        {
            return new Customer
            {
                Address = String.Format("Address of {0}", customerName),
                CustomerName = customerName,
                PhoneNumber = "123456"
            };
        }

        protected Order CreateDummyOrder(Customer customer, PaymentMethod paymentMethod
                                        , DeliveryMethod deliveryMethod, OrderStatus orderStatus)
        {
            return new Order
            {
                Customer = customer,
                DeliveryDate = DateTime.Today.AddDays(7),
                DeliveryMethod = deliveryMethod,
                Freight = "Freight",
                IsDeliveredBefore = false,
                OrderDate = DateTime.Today,
                OrderStatus = orderStatus,
                PaymentMethod = paymentMethod
            };
        }

        protected IList<OrderDetail> CreateDummyOrderDetails(Order order, Composition composition, FabricType fabricType)
        {
            var orderDetails = new List<OrderDetail>();
            for (int i = 0; i < 3; i++)
            {
                orderDetails.Add(new OrderDetail
                {
                    Color = "Black",
                    Composition = composition,
                    CuttableWidth = 100,
                    FabricType = fabricType,
                    IsDone = false,
                    KgQuantity = 1000,
                    MtQuantity = 1000,
                    Order = order,
                    Price = 20000,
                    Weight = 10000
                });
            }
            return orderDetails;
        }

        protected Supplier CreateDummySupplier(string supplierName)
        {
            return new Supplier
            {
                Address = String.Format("Address of {0}", supplierName),
                Phone = "123456",
                SupplierName = supplierName
            };
        }

        protected KnittingOrder CreateDummyKnittingOrder(Order order, Supplier supplier, OrderStatus orderStatus,
                                                        Composition composition, FabricType fabricType)
        {
            return new KnittingOrder
            {
                BelongToOrder = order,
                BelongToSupplier = supplier,
                Composition = composition,
                OrderStatus = orderStatus,
                OrderDate = DateTime.Today,
                DeliveryAddress = "123 Address",
                Diameter = "Nothing",
                ECRUWeight = 10,
                ECRUWidth = 10,
                FabricType = fabricType,
                FabricWeight = 10,
                FinishEstimatedWeight = 10,
                FinishEstimatedWidth = 10,
                Gauge = "Gauge",
                KnittingMachine = "Knitting Machine",
                LenghtOfYarnPer100Needles = 100,
                WhaleCourse = "Nothing"
            };
        }

        protected IList<KnittingOrderDetail> CreateDummyKnittingOrderDetails(KnittingOrder knittingOrder, Composition composition)
        {
            var knittingOrderDetails = new List<KnittingOrderDetail>();
            for (int i = 0; i < 3; i++)
            {
                knittingOrderDetails.Add(new KnittingOrderDetail
                                                                {
                                                                    BelongToKnittingOrder = knittingOrder,
                                                                    Composition = composition,
                                                                    Length = 100,
                                                                    Note = "Note",
                                                                    Price = 1000,
                                                                    Weight = 1000
                                                                });
            }
            return knittingOrderDetails;
        }

        protected DyeOrder CreateDummyDyeOrder(Order order, Supplier supplier, Composition compostion, OrderStatus orderStatus)
        {
            return new DyeOrder
            {
                Article = "Dummy Article",
                BelongToOrder = order,
                BelongToSupplier = supplier,
                OrderStatus = orderStatus,
                Color = "Black",
                Composition = compostion,
                DeliveryAddress = "123 abc 123",
                DeliveryDate = DateTime.Today.AddDays(7),
                DyeInstruction = "Dye Instruction",
                GMS = "GMS",
                Mt = 10,
                OrderDate = DateTime.Today,
                Width = 20
            };
        }

        protected IList<DyeOrderDetail> CreateDummyDyeOrderDetails(DyeOrder order)
        {
            var dyeOrderDetails = new List<DyeOrderDetail>();
            for (int i = 0; i < 3; i++)
            {
                dyeOrderDetails.Add(new DyeOrderDetail
                {
                    BelongToDyeOrder = order,
                    Length = 100,
                    Weight = 100
                });
            }
            return dyeOrderDetails;
        }
    }
}